package com.prs.crm.action.purchase;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.purchase.PurchaseOrder;
import com.prs.crm.domain.purchase.PurchaseOrderDetail;
import com.prs.crm.domain.purchase.PurchaseStoreIn;
import com.prs.crm.domain.purchase.PurchaseStoreInDetail;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.store.StoreIn;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeInvoice;
import com.prs.crm.service.purchase.PurchaseOrderDetailService;
import com.prs.crm.service.purchase.PurchaseOrderService;
import com.prs.crm.service.purchase.PurchaseStoreInService;
import com.prs.crm.service.stock.ProductStoreService;
import com.prs.crm.service.stock.StoreInService;
import com.prs.crm.service.sys.CodeInvoiceService;
import com.prs.crm.service.sys.CodeService;

/**
 * @author 兰小刚
 * @version 创建时间：2010-7-19 下午14:45:10 类说明
 */
public class PurchaseStoreInAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Product[] products;
	private CodeInvoice codeInvoice;
	private Integer[] productNums;
	private Double[] price;
	private Integer[] discount;
	private StoreIn storein;
	private String[] names;
	private String Encode;
	private String storecode;
	private String[] storenumsStrings;
	private Integer[] storeinNums;
	private Integer[] detailid;
	private Integer[] detailstoreNum;
	private CodeService codeService;
	@Autowired
	private StoreInService storeInService;
	private PurchaseStoreIn purchaseStoreIn;
	private PurchaseOrder purchaseOrder;
    private CodeInvoiceService codeInvoiceService;
	private ProductStore productStore;

	private PurchaseStoreInService purchaseStoreInService;

	private ProductStoreService productStoreService;

	private PurchaseOrderService purchaseOrderService;

	private Collection<PurchaseOrderDetail> slist;

	private PaginationSupport pagination;

	@Clew(results = { "success", "none" })
	@Action(value = "savePurchaseStoreIns", results = {
			@Result(name = "success", type = "redirect", location = "listPurchaseStoreIn"),
			@Result(name = "error", type = "chain", location = "editPurchaseOrderToStoreIn"),
			@Result(name = "none", type = "redirect", location = "listPurchaseStoreIn") })
	public String savePurchaseStoreIns() {
		if (purchaseStoreIn.getStoreName() == null
				|| purchaseStoreIn.getStoreName().equals("")) {
			this.addActionError("入库主题不能为空");
			return ERROR;
		}
		if (purchaseStoreIn.getRooms() == null) {
			this.addActionError("仓库不能为空");
			return ERROR;
		}
		if (purchaseStoreIn.getBatchNum() == null
				|| purchaseStoreIn.getBatchNum().equals("")) {
			this.addActionError("批号不能为空");
			return ERROR;
		}
		if (purchaseStoreIn.getStoreInDate() == null) {
			this.addActionError("时间不能为空");
			return ERROR;
		}
		if (products == null) {
			this.addActionError("明细不能为空");
			return ERROR;
		}
	
		PurchaseOrder PurchaseOrderObject = this.getPurchaseOrderService()
				.getPurchaseOrder(purchaseOrder.getId());
		storein=new StoreIn();
		for (int i = 0; i < products.length; i++) {
			
			if (productNums[i] != 0) {
				PurchaseStoreInDetail detail1=new PurchaseStoreInDetail();
				detail1.setInNumber(productNums[i]);
				detail1.setProduct(products[i]);
				detail1.setDiscount(discount[i]);
				detail1.setPrice(price[i]);
				detail1.setPurchaseStoreIn(purchaseStoreIn);
				detail1.setOutNum(0);
				detail1.setOutState(0);
				detail1.setBatchNum(purchaseStoreIn.getBatchNum());
				purchaseStoreIn.addDetail(detail1);
				for (PurchaseOrderDetail detail : PurchaseOrderObject.getDetails()) {
					if (detail.getProduct() == products[i]) {
						detail.setInNum(detail.getInNum() + productNums[i]);
						if (detail.getInNum() == detail.getStockNum()) {
							detail.setInType(1);
						}
					}
				}
				ProductStore ProductStoreObject = this.getProductStoreService()
						.getProductStoreObject(products[i],
								purchaseStoreIn.getRooms(),
								purchaseStoreIn.getBatchNum());

				if (ProductStoreObject == null || "".equals(ProductStoreObject)) {
					ProductStore ProductStoreOb = new ProductStore();
					ProductStoreOb.setProduct(products[i]);
					ProductStoreOb.setProductNum(productNums[i]);
					ProductStoreOb.setStorecode(purchaseStoreIn.getRooms()
							.getRoomcode());
					ProductStoreOb.setRoom(purchaseStoreIn.getRooms());
					ProductStoreOb.setBatchNum(purchaseStoreIn.getBatchNum());
					this.getProductStoreService().save(ProductStoreOb);
				} else {
					ProductStoreObject.setProductNum(ProductStoreObject
							.getProductNum()
							+ productNums[i]);
					this.getProductStoreService().savePrStore(
							ProductStoreObject);
				}
			}
			purchaseStoreIn.setOutState(0);
			storein.setStoragedata(purchaseStoreIn.getStoreInDate());
			storein.setRoom(purchaseStoreIn.getRooms());
			storein.setTitlename(purchaseStoreIn.getStoreName());
			storein.setBatchNum(purchaseStoreIn.getBatchNum());
			storein.setPurchaseOrderId(0);
			storein.setFlowstatus(2);
			storein.setProcess(PurchaseOrderObject.getProcess());
			
			if ("n".equals(Encode)) {
				Code codes = new Code();
				codes.setCodeValue(storecode);
				codes.setEntityName(this.getClass().getName());
				codes.setCodeType("手动编码");
				codes.setCodeMolde("库房入库");
				this.getCodeService().save(codes);
				storein.setCode(codes);
				storein.setStoreInCode(storecode);
			} else {
				CodeInvoice Vob = this.getCodeInvoiceService().getVobject(
						codeInvoice.getId());
				Code codes = new Code();
				codes.setEntityName(this.getClass().getName());
				codes.setCodeValue(storein.getStoreInCode());
				Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
				this.getCodeInvoiceService().saveOrUpdate(Vob);
				codes.setCodeType(Vob.getCodePlan());
				codes.setCodeMolde("库房入库");
				this.getCodeService().save(codes);
				storein.setCode(codes);
			}
		}
		this.getPurchaseStoreInService().save(purchaseStoreIn);
		storeinNums=productNums;
		this.storeInService.save(storein, storeinNums, names, products);
		/**
		 * 状态
		 */
//		boolean a = true;
//		for (PurchaseOrderDetail detail : PurchaseOrderObject.getDetails()) {
//			if (detail.getInType() == 0) {
//				a = false;
//				break;
//			}
//		}
//		if (a) {
			PurchaseOrderObject.setPurStoreState(2);
//		}
		this.getPurchaseOrderService().saveOrUpdate(PurchaseOrderObject);
		
		this.setPrompt("入库成功!");
		return SUCCESS;
	}

	@Action("listPurchaseStoreIn")
	public String excute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PurchaseStoreIn.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPurchaseStoreInService().listByPage(
				criteria, pagination));
		return SUCCESS;
	}

	@Action("viewPurchaseStoreIn")
	public String viewPurchaseStoreIn() {
		purchaseStoreIn = this.getPurchaseStoreInService().get(
				purchaseStoreIn.getId());
		purchaseOrder = this.getPurchaseOrderService().get(
				purchaseStoreIn.getPurchaseOrder().getId());
		this.slist = purchaseOrder.getDetails();
		return SUCCESS;
	}

	public PurchaseStoreIn getPurchaseStoreIn() {
		return purchaseStoreIn;
	}

	public void setPurchaseStoreIn(PurchaseStoreIn purchaseStoreIn) {
		this.purchaseStoreIn = purchaseStoreIn;
	}
    
	public CodeInvoiceService getCodeInvoiceService() {
		return codeInvoiceService;
	}

	public void setCodeInvoiceService(CodeInvoiceService codeInvoiceService) {
		this.codeInvoiceService = codeInvoiceService;
	}

	public PurchaseStoreInService getPurchaseStoreInService() {
		return purchaseStoreInService;
	}

	public void setPurchaseStoreInService(
			PurchaseStoreInService purchaseStoreInService) {
		this.purchaseStoreInService = purchaseStoreInService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public ProductStore getProductStore() {
		return productStore;
	}

	public void setProductStore(ProductStore productStore) {
		this.productStore = productStore;
	}

	public ProductStoreService getProductStoreService() {
		return productStoreService;
	}

	public void setProductStoreService(ProductStoreService productStoreService) {
		this.productStoreService = productStoreService;
	}

	public Product[] getProducts() {
		return products;
	}

	public void setProducts(Product[] products) {
		this.products = products;
	}

	public Integer[] getProductNums() {
		return productNums;
	}

	public void setProductNums(Integer[] productNums) {
		this.productNums = productNums;
	}

	public PurchaseOrder getPurchaseOrder() {
		return purchaseOrder;
	}

	public void setPurchaseOrder(PurchaseOrder purchaseOrder) {
		this.purchaseOrder = purchaseOrder;
	}

	public PurchaseOrderService getPurchaseOrderService() {
		return purchaseOrderService;
	}

	public void setPurchaseOrderService(
			PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	public Collection<PurchaseOrderDetail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<PurchaseOrderDetail> slist) {
		this.slist = slist;
	}

	public Double[] getPrice() {
		return price;
	}

	public void setPrice(Double[] price) {
		this.price = price;
	}

	public Integer[] getDiscount() {
		return discount;
	}

	public void setDiscount(Integer[] discount) {
		this.discount = discount;
	}

	public StoreIn getStorein() {
		return storein;
	}

	public void setStorein(StoreIn storein) {
		this.storein = storein;
	}

	public String[] getNames() {
		return names;
	}

	public void setNames(String[] names) {
		this.names = names;
	}

	public String[] getStorenumsStrings() {
		return storenumsStrings;
	}

	public void setStorenumsStrings(String[] storenumsStrings) {
		this.storenumsStrings = storenumsStrings;
	}

	public Integer[] getStoreinNums() {
		return storeinNums;
	}

	public void setStoreinNums(Integer[] storeinNums) {
		this.storeinNums = storeinNums;
	}

	public Integer[] getDetailid() {
		return detailid;
	}

	public void setDetailid(Integer[] detailid) {
		this.detailid = detailid;
	}

	public Integer[] getDetailstoreNum() {
		return detailstoreNum;
	}

	public void setDetailstoreNum(Integer[] detailstoreNum) {
		this.detailstoreNum = detailstoreNum;
	}

	public StoreInService getStoreInService() {
		return storeInService;
	}

	public void setStoreInService(StoreInService storeInService) {
		this.storeInService = storeInService;
	}
	
	
	public CodeInvoice getCodeInvoice() {
		return codeInvoice;
	}

	public void setCodeInvoice(CodeInvoice codeInvoice) {
		this.codeInvoice = codeInvoice;
	}

	public String getStorecode() {
		return storecode;
	}

	public void setStorecode(String storecode) {
		this.storecode = storecode;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}


	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}

}
